<?php

require_once('../include/GeneralTop.php');
require('../include/class/db.php');
require('../include/class/pagination.php');

if (!$super_admin) {
	die('<div style="text-align:center">Permission denied...</div>');
}

$db = new db;
if ((isset($_GET['member_id'])) && ($_GET['member_id'] != "" && $_GET['action'] == 'del')) {
    $deleteSQL = sprintf("DELETE FROM member_info WHERE member_id=%s",
                       GetSQLValueString($_GET['member_id'], "int"));

    $db->query($deleteSQL);
}

//update to paid member
if ((isset($_GET['member_id'])) && ($_GET['member_id'] != "" && $_GET['action'] == 'pay')) {
    $updateSQL = sprintf("update member_info set Member_type=".MEMBER_PAID.", Member_Status=4, role=".MEMBER_PAID." where member_id=%s",
                       GetSQLValueString($_GET['member_id'], "int"));

    $db->query($updateSQL);
}

//update to volunteer member
if ((isset($_GET['member_id'])) && ($_GET['member_id'] != "" && $_GET['action'] == 'vol')) {
    $updateSQL = sprintf("update member_info set Member_type=".MEMBER_VOL.", Member_Status=3, role=".MEMBER_ORDINARY." where member_id=%s",
                       GetSQLValueString($_GET['member_id'], "int"));    

    $db->query($updateSQL);
    
    $checkSQL = sprintf("select Volunteer_Type from member_info where member_id=%s",
                       GetSQLValueString($_GET['member_id'], "int"));
    $res = $db->query($checkSQL);
    $row = $db->fetch($res);
    if(5 === (int)$row['Volunteer_Type']) {
    	$updateRoleSQL = sprintf("update member_info set role=".MEMBER_CONTENT_ADMIN." where member_id=%s",
                       GetSQLValueString($_GET['member_id'], "int"));    

    	$db->query($updateRoleSQL);
    }
}

//update to ordinary member
if ((isset($_GET['member_id'])) && ($_GET['member_id'] != "" && $_GET['action'] == 'ordinary')) {
    $updateSQL = sprintf("update member_info set Member_type=".MEMBER_ORDINARY.", Member_Status=2, role=".MEMBER_ORDINARY." where member_id=%s",
                       GetSQLValueString($_GET['member_id'], "int"));

    $db->query($updateSQL);
}

include('AdminPanel.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="70%" border="0" align="center" cellpadding="5" cellspacing="0" class="table_border_grey" style="margin-top:10px;">
  <tr>
  	<td colspan="12"><a href="Export.php?action=member">导出Member数据</a></td>
  </tr>
  <tr>
    <td colspan="12">
	  <form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
	  搜索会员: &nbsp;会员姓名 <input type="text" name="name" size="20"> &nbsp;
	  所在区 
	  <select name="district" id="district">
        <option value="all">所有</option>
        <?php
        foreach($district_arr as $_key => $_value) {
        	if($_GET['district'] == $_value) {
        		echo '<option value="'.$_value.'" selected>'.$_value.'</option>';
        	}
        	else {
        		echo '<option value="'.$_value.'">'.$_value.'</option>';
        	}
        }        
        ?>
      </select> &nbsp;
     <?php
	  if((int)$_GET['vol_type'] === '1') {
	  	  $select1 = 'selected';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if((int)$_GET['vol_type'] === '2') {
	  	  $select1 = '';
	  	  $select2 = 'selected';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if((int)$_GET['vol_type'] === '3') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = 'selected';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if((int)$_GET['vol_type'] === '4') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = 'selected';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if((int)$_GET['vol_type'] === '5') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = 'selected';
	  	  $select6 = '';
	  }
	 else if((int)$_GET['vol_type'] === '6') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = 'selected';
	  }
	  ?> 
      志愿者类型 
      <select name="vol_type" id="vol_type">
        <option value="all">所有</option>
        <option value="1" <?php echo $select1?>>救助志愿者</option>
        <option value="2" <?php echo $select2?>>寄养志愿者</option>
        <option value="3" <?php echo $select3?>>运输志愿者</option>
        <option value="4" <?php echo $select4?>>活动策划志愿者</option>
        <option value="5" <?php echo $select5?>>网站志愿者</option>
        <option value="6" <?php echo $select6?>>助养志愿者</option>
      </select> &nbsp;
      工作和义工经验 <input type="text" name="experience" size="20"> &nbsp;
	  <input type="submit" name="submit" id="submit" value=" 搜索 ">
	  </form>
	</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>登录名</td>
    <td>会员姓名</td>
    <td>性别</td>
    <td>email</td>
    <td>地址</td>
    <td>区县</td>
    <td>职业</td>
    <td>会员状态</td>
    <td>会员类型</td>
    <td>&nbsp;</td>
  </tr>
    <?php
	if(isset($_GET['submit'])) {
		$where = '';
		if(isset($_GET['name']) && '' != $_GET['name']) {
			$where .= " and member_login like '%".$_GET['name']."%' ";
		}
		if(isset($_GET['district']) && 'all' != $_GET['district']) {
			$where .= " and Distict = '".$_GET['district']."' ";
		}
		if(isset($_GET['vol_type']) && 'all' != $_GET['vol_type']) {
			$where .= " and Volunteer_Type = '".$_GET['vol_type']."' ";
		}
		if(isset($_GET['experience']) && '' != $_GET['experience']) {
			$where .= " and Experience like '%".$_GET['experience']."%' ";
		}
		$sql = "SELECT * FROM member_info where member_id != 1 ".$where." order by member_id DESC";
	}
	else {
		$sql = "SELECT * FROM member_info where member_id != 1 order by member_id DESC";
	}
	$res = $db->query($sql);
	$num = $db->num($res);
	if($num > 0) {
		$pagination = new Pagination($num, PAGICATION_RECORD_COUNT_NEWS, $_GET['page'], PAGINATION_DELTA);
		$sql = $sql." limit ".$pagination->begin.",".$pagination->end;
		$res = $db->query($sql);
		while($row = $db->fetch($res)) {			
    ?>
    <tr>
      <td>
        <label>
          <input type="button" name="button" id="button" value="Delete" onclick="if(confirm('Are you sure to delete this?')){window.location.href='<?php echo $_SERVER['PHP_SELF']?>?action=del&member_id=<?php echo $row['member_id']; ?>'}"/>
        </label>
      </td>
      <td><a href="EditMember.php?member_id=<?php echo $row['member_id'] ?>">Edit...</a></td>
      <td><a href="ShowDetailMember.php?recordID=<?php echo $row['member_id']; ?>"><?php echo $row['member_login']; ?></a></td>
      <td><?php echo $row['member_name']; ?></td>
      <td><?php echo getMemberSex($row['Sex']); ?></td>
      <td><?php echo $row['Email_account']; ?></td>
      <td><?php echo $row['Address']; ?></td>
      <td><?php echo $row['Distict']; ?></td>
      <td><?php echo $row['profession_type']; ?></td>
      <td><?php echo getMemberStatus($row['Member_Status']); ?></td>
      <td><?php echo getMemberType($row['Member_type']); ?></td>
      <td>
        <?php
        if(MEMBER_PAID !== (int)$row['Member_type']) {
        ?>
        <a href="ShowMember.php?action=pay&member_id=<?php echo $row['member_id'] ?>">升级为付费会员</a>&nbsp;&nbsp;
        <?php
        }
       	if(MEMBER_VOL !== (int)$row['Member_type']) {
        ?>
        <a href="ShowMember.php?action=vol&member_id=<?php echo $row['member_id'] ?>">升级为志愿者</a>
        <?php
       	}
		if(MEMBER_ORDINARY !== (int)$row['Member_type']) {
        ?>
        <a href="ShowMember.php?action=ordinary&member_id=<?php echo $row['member_id'] ?>">改为普通会员</a></td>
        <?php
       	}
       	?>
    </tr>
    <?php
		}
    }
    ?>
    <tr>
    <td colspan="12">
	    <?php
		if($num > PAGICATION_RECORD_COUNT_NEWS) {
			echo $pagination->paginationString;
		}
		?>
	</td>
  </tr>
</table>
<br />
</body>
</html>